﻿using HalconDotNet;
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Media;

namespace DetectApp.Display;

public static class HObj2Draw
{
    public static DrawingVisual Cross2Draw(double row, double col, double size, double angle, Color color, double lineWidth)
    {
        PathFigure pathFigure1 = new() { StartPoint = new Point(-size / 2 + col, row), IsClosed = false, IsFilled = false, };
        pathFigure1.Segments.Add(new LineSegment { Point = new(size / 2 + col, row), IsStroked = true });
        PathFigure pathFigure2 = new() { StartPoint = new(col, -size / 2 + row), IsClosed = false, IsFilled = true, };
        pathFigure2.Segments.Add(new LineSegment { Point = new(col, size / 2 + row), IsStroked = true });
        PathGeometry pathGeometry = new();
        pathGeometry.Figures.Add(pathFigure1);
        pathGeometry.Figures.Add(pathFigure2);
        DrawingVisual drawingVisual = new();
        using (var dc = drawingVisual.RenderOpen())
        {
            Pen drawingpen = new(new SolidColorBrush(color), lineWidth);
            dc.DrawGeometry(new SolidColorBrush(color), drawingpen, pathGeometry);
        }
        drawingVisual.Transform = new RotateTransform(-angle, col, row);
        return drawingVisual;
    }

    public static DrawingVisual Region2Draw(HRegion region, Brush brush, bool isFill, double lineWidth)
    {
        DrawingVisual drawingVisual = new();
        DrawingContext dc = drawingVisual.RenderOpen();
        GeometryGroup geometryGroup = new GeometryGroup();
        HXLDCont cont = region.GenContourRegionXld("border_holes");

        for (int i = 1; i < cont.CountObj() + 1; i++)
        {
            cont[i].GetContourXld(out HTuple rows, out HTuple cols);
            List<Point> points = new();
            for (int j = 1; j < rows.Length - 1; j++)
            {
                points.Add(new Point { X = cols[j], Y = rows[j], });
            }

            PathGeometry pathGeometry = new();
            PathFigure pathFigure = new PathFigure();
            pathFigure.StartPoint = new Point(cols[0], rows[0]);
            pathFigure.Segments.Add(new PolyLineSegment(points, true));
            pathFigure.IsClosed = true;
            pathFigure.IsFilled = isFill;

            pathGeometry.Figures.Add(pathFigure);
            geometryGroup.Children.Add(pathGeometry);
        }
        Pen drawingPen = new(brush, lineWidth);
        dc.DrawGeometry(brush, drawingPen, geometryGroup);
        dc.Close();
        return drawingVisual;
    }

    public static DrawingVisual Region2Draw(List<HRegion> regions, Brush brush, bool isFill)
    {
        DrawingVisual drawingVisual = new();
        DrawingContext dc = drawingVisual.RenderOpen();
        regions.ForEach(region =>
        {
            HXLDCont cont = region.GenContourRegionXld("border_holes");
            for (int i = 1; i < cont.CountObj() + 1; i++)
            {
                cont[i].GetContourXld(out HTuple rows, out HTuple cols);
                List<Point> points = new();
                for (int j = 1; j < rows.Length - 1; j++)
                {
                    points.Add(new Point { X = cols[j], Y = rows[j], });
                }
                Pen drawingPen = new(brush, 3);
                PathGeometry pathGeometry = new();
                PathFigure pathFigure = new PathFigure();
                pathFigure.StartPoint = new Point(cols[0], rows[0]);
                pathFigure.Segments.Add(new PolyLineSegment(points, true));
                pathFigure.IsClosed = true;
                pathFigure.IsFilled = isFill;
                pathGeometry.Figures.Add(pathFigure);
                dc.DrawGeometry(brush, drawingPen, pathGeometry);
            }
        });

        dc.Close();

        return drawingVisual;
    }

    public static DrawingVisual Text2Visual(string text, Color color, double fontsize, Point origin)
    {
        FormattedText formattedText = new(text, CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, new Typeface("Consolas"), fontsize, new SolidColorBrush(color));
        DrawingVisual drawingVisual = new();
        using var dc = drawingVisual.RenderOpen();
        dc.DrawText(formattedText, origin);
        return drawingVisual;

    }

    public static DrawingVisual Xld2Draw(HXLDCont cont, Brush brush, double lineWidth, bool IsClosed)
    {
        DrawingVisual drawingVisual = new();
        DrawingContext dc = drawingVisual.RenderOpen();
        GeometryGroup geometryGroup = new GeometryGroup();

        for (int i = 1; i < cont.CountObj() + 1; i++)
        {
            cont[i].GetContourXld(out HTuple rows, out HTuple cols);
            List<Point> points = new();
            for (int j = 1; j < rows.Length - 1; j++)
            {
                points.Add(new Point { X = cols[j], Y = rows[j], });
            }

            PathGeometry pathGeometry = new();
            PathFigure pathFigure = new()
            {
                StartPoint = new Point(cols[0], rows[0]),
                IsClosed = IsClosed,
                IsFilled = false,
            };
            pathFigure.Segments.Add(new PolyLineSegment(points, true));

            pathGeometry.Figures.Add(pathFigure);
            geometryGroup.Children.Add(pathGeometry);
        }
        Pen drawingPen = new(brush, lineWidth);
        dc.DrawGeometry(brush, drawingPen, geometryGroup);
        dc.Close();
        return drawingVisual;
    }

    public static DrawingVisual Xld2Draw(List<HXLDCont> conts, Brush brush, double lineWidth, bool IsClosed)
    {
        DrawingVisual drawingVisual = new();
        DrawingContext dc = drawingVisual.RenderOpen();
        GeometryGroup geometryGroup = new GeometryGroup();
        foreach (var cont in conts)
        {
            for (int i = 1; i < cont.CountObj() + 1; i++)
            {
                cont[i].GetContourXld(out HTuple rows, out HTuple cols);
                List<Point> points = new();
                for (int j = 1; j < rows.Length - 1; j++)
                {
                    points.Add(new Point { X = cols[j], Y = rows[j], });
                }

                PathGeometry pathGeometry = new();
                PathFigure pathFigure = new()
                {
                    StartPoint = new Point(cols[0], rows[0]),
                    IsClosed = IsClosed,
                    IsFilled = false,
                };
                pathFigure.Segments.Add(new PolyLineSegment(points, true));

                pathGeometry.Figures.Add(pathFigure);
                geometryGroup.Children.Add(pathGeometry);
            }
            Pen drawingPen = new(brush, lineWidth);
            dc.DrawGeometry(brush, drawingPen, geometryGroup);
        }

        dc.Close();
        return drawingVisual;
    }
}